package com.jay.tools.base;

import android.os.Bundle;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import com.github.zackratos.ultimatebar.UltimateBar;
import com.jay.tools.R;
import com.jay.tools.utils.ToastUtil;

import me.yokeyword.fragmentation.SupportActivity;
import me.yokeyword.fragmentation.core.anim.DefaultNoAnimator;
import me.yokeyword.fragmentation.core.anim.FragmentAnimator;

/**
 * author: tanjianpei
 * date: 2019/8/19 019 15:05
 * description: 宿主Activity父类
 */
public abstract class ProxyActivity extends SupportActivity {

    protected String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        UltimateBar.Companion.with(this)
                .statusDark(false)                  // 状态栏灰色模式(Android 6.0+)，默认 flase
                .applyNavigation(false)              // 应用到导航栏，默认 flase
                .navigationDark(true)              // 导航栏灰色模式(Android 8.0+)，默认 false
                .create()
                .immersionBar(); // 状态栏透明

        initContainer(savedInstanceState, setRootFragment());
        initDatas(savedInstanceState);
    }

    protected abstract BaseFragment setRootFragment();

    private void initContainer(@Nullable Bundle savedInstanceState, BaseFragment fragment) {
        FrameLayout container = new FrameLayout(this);
        //fragment_container这样设置的id在Android中是独有的，绝不会重复
        container.setId(R.id.fragment_container);

        setContentView(container);

        if (null == savedInstanceState) {
            loadRootFragment(R.id.fragment_container, fragment);
        }
    }

    protected abstract void initDatas(Bundle savedInstanceState);

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.gc();
        System.runFinalization();
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        return new DefaultNoAnimator();// Fragment切换时无动画
    }

    protected void toast(String msg) {
        ToastUtil.toast(this, msg);
    }

    protected void toast(int msg) {
        ToastUtil.toast(this, msg);
    }

}
